﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

using System.Drawing;
using Blue.ResourceManager;
using System.IO;
using EngineOfEvermore.Scene;
using EngineOfEvermore.Common;
using EngineOfEvermore.Scene.Visual;
using EngineOfEvermore.Imaging;
using EngineOfEvermore.Scene.Physics;
using System.Diagnostics;
using EngineOfEvermore.Testing;

namespace EngineOfEvermore.GameApp.GameStates
{
    class GameStatePlaying : GameState
    {
        SceneGraph _sceneGraph;
        System.ComponentModel.Design.ServiceContainer _serviceProvider;
        SceneObject _somboy;
        float _stateTime;

        public GameStatePlaying() :
            base( "GameStatePlaying" )
        {
            _serviceProvider = new System.ComponentModel.Design.ServiceContainer();
        }

        public override void Draw( IRenderer renderer )
        {
            Camera cam = (Camera) renderer.Camera;

            cam.ApplyTransform();
            _sceneGraph.Draw( new RectangleF( -10000, -10000, 20000, 20000 ), renderer );

            cam.Position = _somboy.Position;
        }

        public override void Update( LogicUpdateEventArgs e )
        {
            _sceneGraph.Update( e );
            _stateTime += e.ElapsedTime;
        }

        public override void OnActivate( GameState previousState )
        {
            _sceneGraph = ( new TestSceneFactory().CreateTestScene( _serviceProvider ) );
            _addPhysicsDebugRenderingComponentsToAllObjects();

            _somboy = _sceneGraph.SceneObjects
                .Single( so => so.GetComponent<Testing.TestCharacterController>() != null );


            //-------------------------- Rotation Test Objects ------------------------------------

            // Static / No clipping
            var rotationTestObject_StaticNoClipping = 
                RotationTester.CreateStaticSpriteTestObject();
            rotationTestObject_StaticNoClipping.Position = new Vector2d( 100, 200 );

            _sceneGraph.AddSceneObject( rotationTestObject_StaticNoClipping );

            // Animated / No clipping
            var rotationTestObject_AnimatedNoClipping =
                RotationTester.CreateAnimatedSpriteTestObject();
            rotationTestObject_AnimatedNoClipping.Position = new Vector2d( 100, 300 );

            _sceneGraph.AddSceneObject( rotationTestObject_AnimatedNoClipping );

            // Static / With clipping
            var rotationTestObject_StaticWithClipping =
                RotationTester.CreateStaticSpriteTestObject();
            rotationTestObject_StaticWithClipping.Position = new Vector2d( 0, 200 );
            rotationTestObject_StaticWithClipping
                .GetComponent<VisualComponent>()
                .Children
                .OfType<StaticSprite>()
                .Single()
                .ClipArea = new RectangleF( 0, 0.5f, 1.0f, 0.5f );    

            _sceneGraph.AddSceneObject( rotationTestObject_StaticWithClipping );


            // Animated / With clipping
            var rotationTestObject_AnimatedWithClipping =
                RotationTester.CreateAnimatedSpriteTestObject();
            rotationTestObject_AnimatedWithClipping.Position = new Vector2d( 0, 300 );
            rotationTestObject_AnimatedWithClipping
                .GetComponent<VisualComponent>()
                .Children
                .OfType<AnimatedSprite>()
                .Single()
                .ClipArea = new RectangleF( 0, 0.5f, 1.0f, 0.5f );

            _sceneGraph.AddSceneObject( rotationTestObject_AnimatedWithClipping );
        }

        public override void OnDeActivate( GameState nextState )
        {
            _sceneGraph.OnDeInit( _serviceProvider );
            _sceneGraph = null;
        }

        public override void OnInit()
        {
            base.OnInit();
        }

        public override void OnDeInit()
        {
            base.OnDeInit();
        }

        private void _addPhysicsDebugRenderingComponentsToAllObjects()
        {
            foreach ( var sceneObject in _sceneGraph.SceneObjects )
            {
                if ( sceneObject.GetComponent<PhysicsDebugRenderingComponent>() == null )
                {
                    sceneObject.AddComponent( new PhysicsDebugRenderingComponent() );
                }
            }
        }
    }

    
}
